﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QuanLyNhaSach.Employee
{
    public partial class FrmUsersGroupDetail : DevComponents.DotNetBar.Office2007Form
    {
        UsersGroupBUS usersGroupBus;

        public ActionName Action { get; set; }
        public UsersGroupDTO UsersGroup { get; set; }

        public FrmUsersGroupDetail()
        {
            InitializeComponent();
            usersGroupBus = new UsersGroupBUS();
            btnUpdate.Location = btnAdd.Location;
        }

        private void FrmUsersGroupDetail_Load(object sender, EventArgs e)
        {
            if (Action == ActionName.Insert)
            {
                txtUsersGroupCode.Focus();
                UsersGroup = new UsersGroupDTO();
            }
            else
            {
                txtUsersGroupCode.Text = UsersGroup.UsersGroupCode;
                txtUsersGroupName.Text = UsersGroup.UsersGroupName;
            }

            ProcessButton();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            UsersGroup.UsersGroupCode = txtUsersGroupCode.Text;
            UsersGroup.UsersGroupName = txtUsersGroupName.Text;
            UsersGroup.Status = true;
            if (!CheckDataInput())
            {
                return;
            }

            UsersGroup.UsersGroupID = usersGroupBus.InsertUsersGroup(UsersGroup);
            if (UsersGroup.UsersGroupID > 0)
            {
                Global.SetMessage(lblMessage, "Thêm thành công!", true);
                Action = ActionName.Update;
                ProcessButton();

                Employee.FrmUserGroupList frmList = (Employee.FrmUserGroupList)Application.OpenForms["FrmUserGroupList"];
                if (frmList != null)
                {
                    frmList.UsersGroupChangeAfterInsert(UsersGroup);
                }

                RuleObjectBUS RuleBUS = new RuleObjectBUS();
                RuleBUS.InsertRuleOfGroup(UsersGroup.UsersGroupID);
            }
            else
            {
                Global.SetMessage(lblMessage, "Thêm không thành công!", false);
            }
        }

        private bool CheckDataInput()
        {
            if (string.IsNullOrEmpty(txtUsersGroupCode.Text))
            {
                Global.SetMessage(lblMessage, "Mã nhóm không được trống!", false);
                txtUsersGroupCode.Focus();
                return false;
            }
            else
                if(string.IsNullOrEmpty(txtUsersGroupName.Text))
                {
                     Global.SetMessage(lblMessage, "Tên nhóm không được trống!", false);
                      txtUsersGroupName.Focus();
                       return false;
                }
                else
                    if (txtUsersGroupCode.Text.Length < 2)
                    {
                        Global.SetMessage(lblMessage, "Mã nhóm phải từ 2 đến 4 kí tự!", false);
                        txtUsersGroupCode.Focus();
                        return false;
                    }
            
                return true;
        }

        public void ResetForm()
        {
            txtUsersGroupName.Text = string.Empty;
            txtUsersGroupCode.Text = string.Empty;
            txtUsersGroupCode.Focus();
        }


        #region Button

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            ResetForm();
            Action = ActionName.Insert;
            ProcessButton();
            UsersGroup = new UsersGroupDTO();
        }

        private void ProcessButton()
        {
            if (Action == ActionName.Insert)
            {
                btnUpdate.Visible = false;
                btnAdd.Visible = true;
            }
            else if (Action == ActionName.Update)
            {
                btnUpdate.Visible = true;
                btnAdd.Visible = false;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!CheckDataInput())
            {
                return;
            }

            UsersGroup.UsersGroupName = txtUsersGroupName.Text;
            UsersGroup.UsersGroupCode = txtUsersGroupCode.Text;

            if (usersGroupBus.UpdateUsersGroup(UsersGroup))
            {
                Global.SetMessage(lblMessage, "Cập nhật thành công!", true);
                //OnCategoryChanged(Category);
                Employee.FrmUserGroupList frmList = (Employee.FrmUserGroupList)Application.OpenForms["FrmUsersGroupList"];
                if (frmList != null)
                {
                    frmList.UsersGroupChanged(UsersGroup);
                }
            }
            else
            {
                Global.SetMessage(lblMessage, "Cập nhật không thành công!", false);
            }
        }

        #endregion Button

        private void UsersGroupDetail_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                switch (Action)
                {
                    case ActionName.Insert:
                        btnAdd_Click(sender, e);
                        break;
                    case ActionName.Update:
                        btnUpdate_Click(sender, e);
                        break;
                }
            }
        }

    }
}
